package com.pda.yuanjing.pages.jdpd.add;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.pda.yuanjing.PDApp;
import com.pda.yuanjing.R;
import com.pda.yuanjing.beanin.AddCBillBeanIn;
import com.pda.yuanjing.beanout.JDBeanNewOut;
import com.pda.yuanjing.beanout.PDPBeanOut;
import com.pda.yuanjing.beanout.PPBeanOut;
import com.pda.yuanjing.database.InStore;
import com.pda.yuanjing.dialog.product.ProductDialog;
import com.pda.yuanjing.dialog.store.StoreDialog;
import com.pda.yuanjing.pages.sytj.TJActivity;
import com.public_module.basemvp.activity.BaseActivity;
import com.public_module.tools.DateUtils;
import com.public_module.tools.SharedPreferencesManger;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class JDBillAddActivity extends BaseActivity<AddBillModel, AddBillView, AddBillPresent> implements AddBillView {
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.rltitle)
    RelativeLayout rltitle;
    @BindView(R.id.tvbill)
    EditText tvbill;
    @BindView(R.id.tvstore)
    TextView tvstore;
    @BindView(R.id.ll1)
    LinearLayout ll1;
    @BindView(R.id.tvproduct)
    TextView tvproduct;
    @BindView(R.id.ll2)
    LinearLayout ll2;
    @BindView(R.id.edcode)
    EditText edcode;
    @BindView(R.id.logine)
    Button logine;
    @BindView(R.id.ll3)
    LinearLayout ll3;
    @BindView(R.id.listview)
    SwipeMenuListView listview;
    @BindView(R.id.tvb)
    TextView tvb;
    @BindView(R.id.tvbatch)
    EditText tvbatch;

    private String TAG = "BillAddActivity";
    private OptionsPickerView pickerView;
    JDBeanNewOut.DataBean dataBean = null;
    String pid = "-1";
    String pname = "";
    ProductAdapter productAdapter = null;
    List<PDPBeanOut.DataBean> listt = new ArrayList<>();

    protected void initdata() {
        dataBean = (JDBeanNewOut.DataBean) getIntent().getSerializableExtra(PDApp.process);
        tvTitle.setText(dataBean.getJieDianName() + " " + SharedPreferencesManger.getRealName());
        tvbill.setText(dataBean.getProcessID() + DateUtils.stampToDateTime(new Date().getTime()));
        View headView = getLayoutInflater().inflate(R.layout.head_isscanf, null);
        listview.addHeaderView(headView, null, false);
        listview.setHeaderDividersEnabled(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addjd);
        ButterKnife.bind(this);
        initdata();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick({R.id.iv_title_left, R.id.logine, R.id.tv_right, R.id.tvproduct, R.id.tvstore})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.tv_right:
                if (tvbill.getText().toString().length() == 0) {
                    showMsg("请填写单号");
                    return;
                }
//                if (tvbatch.getText().toString().length() == 0) {
//                    showMsg("请填写批次");
//                    return;
//                }
                if (listt == null || listt.size() == 0) {
                    showMsg("当前无产品");
                    return;
                }
                AddCBillBeanIn addCBillBeanIn = new AddCBillBeanIn();
                addCBillBeanIn.setCid(SharedPreferencesManger.getCid());
                addCBillBeanIn.setAction("SY_AddBatch");
                addCBillBeanIn.setUserID(SharedPreferencesManger.getUid());
                addCBillBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
                addCBillBeanIn.setBillCode(tvbill.getText().toString());
                addCBillBeanIn.setJieDianName(pname);
                addCBillBeanIn.setProcessID(pid);
                addCBillBeanIn.setBillType("900");
                addCBillBeanIn.setGongYingShangID("-1");
                addCBillBeanIn.setStoreIDIn("-1");
                addCBillBeanIn.setBatch(tvbatch.getText().toString());
                for (int i = 0; i < listt.size(); i++) {
                    AddCBillBeanIn.DataBean dataBean = new AddCBillBeanIn.DataBean();
                    dataBean.setPlanNums(listt.get(i).getNums());
                    dataBean.setProductID(listt.get(i).getProductID());
                    dataBean.setProductName(listt.get(i).getProductInfo());
                    addCBillBeanIn.getListProduct().add(dataBean);
                }
                presenter.addList(addCBillBeanIn);
                break;
            case R.id.tvstore:
                List<PPBeanOut.DataBean> options1Items = new ArrayList<>();
                List<List<String>> options2Items = new ArrayList<>();
                List<PPBeanOut.DataBean> listt = new ArrayList<>();
                listt.addAll(PDApp.ppBeanOut.getData());
                for (int i = 0; i < listt.size(); i++) {
                    PPBeanOut.DataBean pt = listt.get(i);
                    options1Items.add(pt);
                    List<String> ptl = new ArrayList<>();
                    for (int j = 0; j < listt.get(i).getListJieDianName().size(); j++) {
                        ptl.add(listt.get(i).getListJieDianName().get(j));
                    }
                    options2Items.add(ptl);
                }
                pickerView = new OptionsPickerBuilder(JDBillAddActivity.this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String tx = options1Items.get(options1).getPickerViewText() + "-"
                                + options2Items.get(options1).get(options2);
                        pid = options1Items.get(options1).getProcessID();
                        pname = options2Items.get(options1).get(options2);
                        tvstore.setText(tx);
                        AddCBillBeanIn addCBillBeanIn = new AddCBillBeanIn();
                        addCBillBeanIn.setCid(SharedPreferencesManger.getCid());
                        addCBillBeanIn.setAction("SY_DownloadKuCunForJieDianPD");
                        addCBillBeanIn.setUserID(SharedPreferencesManger.getUid());
                        addCBillBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
                        addCBillBeanIn.setStoreIDIn("-1");
                        addCBillBeanIn.setProcessID(pid);
                        addCBillBeanIn.setJieDianName(pname);
                        presenter.getPlist(addCBillBeanIn);
                    }
                })
                        .setTitleText("选择生产流程")
                        .setContentTextSize(20)//设置滚轮文字大小
                        .setTitleSize(20)
                        .setSubCalSize(20)
                        .setDividerColor(Color.LTGRAY)//设置分割线的颜色
                        .setSelectOptions(0)//默认选中项
                        .setBgColor(Color.WHITE)
                        .setTitleBgColor(getResources().getColor(R.color.white))
                        .setTitleColor(getResources().getColor(R.color.black))
                        .setCancelColor(getResources().getColor(R.color.gray_high))
                        .setSubmitColor(getResources().getColor(R.color.blue))
                        .setTextColorCenter(getResources().getColor(R.color.black))
                        .setSubmitText("确定")
                        .setCancelText("取消")
                        .isRestoreItem(true)//切换时是否还原，设置默认选中第一项。
                        .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                        .setOutSideColor(0x2A2A2A4F) //设置外部遮罩颜色
                        .build();
                pickerView.setPicker(options1Items, options2Items);
                pickerView.show();
                break;
        }
    }

    @Override
    public void tip(String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (str.length() == 0) {
                    showMsg("订单添加完成");
                    finish();
                } else {
                    showMsg(str);
                }
            }
        });
    }

    @Override
    public void tipS(List<PDPBeanOut.DataBean> list) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                listt = list;
                if (listt != null) {
                    productAdapter = new ProductAdapter(JDBillAddActivity.this, listt);
                    listview.setAdapter(productAdapter);
                }
            }
        });
    }

    @Override
    public AddBillModel createModel() {
        return new AddBillModelImpl(this);
    }

    @Override
    public AddBillView createView() {
        return this;
    }

    @Override
    public AddBillPresent createPresenter() {
        return new AddBillPresent();
    }
}
